/*******************************************************************************
********************************************************************************
**************** ZOC Program Evaluation Analysis *******************************
********************************************************************************
********************************************************************************
* Chris Campos
* 01-05-2022
*
* Last updated: 08-2023
* 
* Runtime: 6 minutes 
* *****************************************************************************/
timer clear 
timer on 1 
set trace on
set tracedepth 2
set matsize 10000

grstyle init
grstyle set legend 6, nobox

capture program drop main
program define main

  global user "cqcampos"
  paths

  * Main analysis dataset (non-ITT)
  local analysis_ela "$datadir/zoc_analysis_data_ela"
  local analysis_math "$datadir/zoc_analysis_data_math"

  * Make Figure 1 
  zocMap

  * Make Figure 2
  choseNonNeighborhoodDescriptive

  * Make Table 1 
  descriptive_stats, data(`analysis_ela')

  * Make Figure 3
  * Figure 3a
  event_study, data(`analysis_ela') traditional("No") subjectMath("FALSE")

  * Figure 3b
  college_event_study,  data(`school_chars') traditional("No")
  * Figure 3c
  event_study_itt, groupfe("middlecensusblockid")

  * Figure 3d 
  college_event_study_itt, groupfe("middlecensusblockid")

  *  Figure 4
  vaQTE, data(`analysis_ela') 

    * Table 1 - Descriptive stats
  descriptive_stats, data(`analysis_ela')

    * Table 2 - Prefs 
   deltaRegressions
   deltaRegressions, robustness("robust")

  * Table 3 - OVG 
  * Panel A
   ovgRobustness, data(`analysis_ela')  subjectMath("FALSE") prevar("post_zoc") postvar("post_zoc_highOVG_3_4") group("highOVG_3_4") outcome("z_ela_all") yearfe("endyear")
  * Panel B
   ovgRobustness, data(`analysis_ela')  subjectMath("FALSE") prevar("post_zoc") postvar("postindividualovg") group("ovg_estimate") outcome("z_ela_all") yearfe("endyear")
  * Panel C 
   ovgRobustness, data(`analysis_ela')  subjectMath("FALSE") prevar("post_zoc") postvar("post_schoolovgestimate") group("") outcome("z_ela_all") yearfe("endyear")

    * Table 4 - Additional outcomes 
   intermediateOutcomes, data(`analysis_ela')



end

************************************************************************************************
************************************************************************************************
* paths
*
* Declare paths for the analysis 
************************************************************************************************
************************************************************************************************

  capture program drop paths
  program define paths
    
    if "$user" == "cqcampos" {
      global dir "DIRECTORY1"
    }

    global datadir "$dir/data"

    global tables $dir/tables
    global codeR $dir/codeR
    global code $dir/code
    * Where to save clean data
    global rawdata $dir/rawdata
    global figures $dir/figures
    global logs $dir/logs


  end
  paths

************************************************************************************************
************************************************************************************************
* Figure 1 - ZOC Map
*
************************************************************************************************
************************************************************************************************
capture program drop zocMap 
program define zocMap 

  local script "$code/makeZOCMap.R"
  shell /Library/Frameworks/R.framework/Resources/bin/R --vanilla <`script'

end 

************************************************************************************************
************************************************************************************************
* Figure 2 - Share choosing non-neighborhood school
*
************************************************************************************************
************************************************************************************************
capture program drop choseNonNeighborhoodDescriptive
program define choseNonNeighborhoodDescriptive

  * Ninth grade enrollment
  use $datadir/lausd2002_2021, clear 
  keep if gradecode==9
  rename preferredlocationcode schoolin9 
  keep studentpseudoid endyear schoolin9  
  tempfile schoolin9 
  save `schoolin9'

  * link with application data 
  use if endyear>=2013 & endyear<=2018 using $datadir/choiceData, clear 
  keep if order==1 
  replace endyear = endyear + 1
  duplicates drop studentpseudoid endyear, force 
  merge 1:1 studentpseudoid endyear using `schoolin9', gen(mergeStudentIn9) keep( 3)
  replace endyear = endyear -1


   * Enrolled school is not their neighborhood-assigned school (assigned_code1)
  gen enrollOther = (schoolin9 != assigned_code1) if !missing(assigned_code1)
  * Top-ranked school is not their neighborhood-assigned school (assigned_code1)
  gen choseOther = assigned_code1 != choice_plocn if !missing(assigned_code1)
  * drop those that we cant assign a code 
  drop if missing(assigned_code1) | missing(schoolin9)
  
  collapse (mean) choseOther  enrollOther  , by( endyear)  
  sort endyear
  twoway (connected choseOther endyear, color(navy) ) ///
          (connected enrollOther endyear, color(gs10) lpattern(dash)), ///
      ytitle("Share") ///
      xtitle("Cohort") ///
      ylabel(0 0.25 0.5 0.75 1) ///
      legend(order(1 "Share choosing non-neighborhood school" 2 "Share enrolling in non-neighborhood school") row(2))
  graph export $figures/share_choosing_nonneighborhood_school.pdf, replace 

end 




************************************************************************************************
************************************************************************************************
* Figure 3a 
*
************************************************************************************************
************************************************************************************************
capture program drop event_study
program define event_study
syntax, [data(string) traditional(string) subjectMath(string)]


  use `data', clear 

  if "`subjectMath'"=="TRUE"{
    local nameappend "_math"
    local outcome "z_math_all"
  }
  if "`subjectMath'"!="TRUE"{
    local outcome "z_ela_all"
    local nameappend "_ela"
  }

  ******************************************
  * Baseline event study
  *
  * Do with and without controls to show
  * robustness
  ******************************************

  * Drop missing test scores (set to zero in build)
  drop if `outcome'==0


  *preserve
  tempfile noP
  tempfile P
  reghdfe `outcome'    b5-a6  hispanic black sped migrant college poverty   female spanish_at_home  trend_*_1   if   maxYear==2019 & minYear<=2011  & endyear >=2008 & analysis_schools==1,  ///
      absorb(preferredlocationcode endyear  ) vce(cluster preferredlocationcode   )  nocons


  parmest, saving(`noP')

  reghdfe `outcome'   b5-a6  hispanic black sped migrant college poverty  female spanish_at_home  trend_*_1   if  maxYear==2019 & minYear<=2011  & endyear >=2008,  ///
      absorb(preferredlocationcode endyear  ) vce(cluster preferredlocationcode    )  nocons

  parmest, saving(`P')

  use `noP', clear

  drop if _n>10

  gen time = substr(parm,2,1)
  destring time, replace
  replace time = -time if substr(parm,1,1) =="b"

  expand 2 in 1
  replace time = -1 if _n==11
  replace est = 0 if _n==11
  replace max95 = 0 if _n==11
  replace min95= 0 if _n==11
    sort time
    gen y = 0
    gen x = 0
  save `noP', replace

  use `P', clear
    drop if _n>10
  gen time = substr(parm,2,1)
  destring time, replace
  replace time = -time if substr(parm,1,1) =="b"

  expand 2 in 1
  replace time = -1 if _n==11
  replace est = 0 if _n==11
  replace max95 = 0 if _n==11
  replace min95= 0 if _n==11
    sort time
    gen y = 0
    gen x = 0

  keep estimate time
  rename estimate estimate_p
  save `P', replace
  merge 1:1 time using `noP'



  twoway (rarea min95 max95 time,  color(navy) fintensity(inten10) lcolor(navy) lpattern(dot)  ) ///
        (line estimate time, lcolor(navy) lpattern(dash) ) ///
        (line estimate_p time, lcolor(navy) lpattern(solid) ) ///
        (function y=0, lcolor(black)  lpattern(dash_dot) range(-5 6)) ///
        (function y = -0.5, lcolor(black) lpattern(dash_dot)  range(-.2 .3) horizontal), ///
         xtitle("Year Relative to ZOC Expansion")  legend(off) ///
         ytitle("Student Achievement Effect ({&sigma})") ///
         xlabel( -5 -3 -1 1 3 5)

  graph export $figures/event_study_student_achievement`nameappend'.pdf, replace
  graph export $figures/event_study_student_achievement`nameappend'.png, replace
  

end


************************************************************************************************
************************************************************************************************
* Figure 3c
*
************************************************************************************************
************************************************************************************************
capture program drop event_study_itt 
program define event_study_itt
syntax, [data(string) groupfe(string)]


    use $datadir/analysis_ela_itt.dta, clear 

    drop if z_ela_all ==0
    preserve

    reghdfe z_ela_all  b5-a6   hispanic black sped migrant college poverty female spanish_at_home  ,  absorb(`groupfe'    endyear ) vce(cluster `groupfe' )

    parmest, norestore
    drop if _n>10

    gen time = substr(parm,2,1)
    destring time, replace
    replace time = -time if substr(parm,1,1) =="b"

    expand 2 in 1
    replace time = -1 if _n==11
    replace est = 0 if _n==11
    replace max95 = 0 if _n==11
    replace min95= 0 if _n==11
    sort time
    gen y = 0
    gen x = 0

    keep estimate time max95 min95 
    rename estimate estimate_p
    rename max95 max95_2
    rename min95 min95_2 

    twoway (rarea min95_2 max95_2 time,  color(navy) fintensity(inten10) lcolor(navy) lpattern(dot)  ) ///
        (line estimate_p time, lcolor(navy) lpattern(solid) ) ///
        (function y=0, lcolor(black)  lpattern(dash_dot) range(-5 6)) ///
        (function y = -0.5, lcolor(black) lpattern(dash_dot)  range(-.2 .3) horizontal) , ///
            xtitle("Year Relative to ZOC Expansion")  legend(off) ///
            ytitle("Student Achievement Effect ({&sigma})") ///
            xlabel( -5 -3 -1 1 3 5 )

    graph export $figures/event_study_student_achievement_ittrobust`groupfe'.pdf, replace
    restore



end 




************************************************************************************************
************************************************************************************************
* Figure 3b
*
************************************************************************************************
************************************************************************************************
capture program drop college_event_study
program define college_event_study
syntax, [data(string) traditional(string)]

  use $datadir/zoc_college_sample.dta, clear 
    

  tempfile matched 
  tempfile unmatched 

  reghdfe four_year    b5-a6 hispanic black college poverty sped female migrant spanish_at_home  trend_*_1 ///
    if   endyear >=2008 & endyear<=2019 , ///
      absorb(preferredlocationcode endyear  ) vce(cluster preferredlocationcode     )  nocons level(95)     
  parmest, saving(`unmatched')

  reghdfe four_year    b5-a6 black college poverty sped female migrant spanish_at_home trend_*_1   ///
    if   endyear >=2008 & endyear<=2019 & analysis_schools==1, ///
      absorb(preferredlocationcode endyear  ) vce(cluster preferredlocationcode     )  nocons level(95)
  parmest, saving( `matched')
  

  preserve
  
  use `unmatched', clear 

  drop if _n>=12
  gen time = substr(parm,2,1)
  destring time, replace
  replace time = -time if substr(parm,1,1) =="b"

  expand 2 in 1
  replace time = -1 if _n==12
  replace est = 0 if _n==12
  replace max95 = 0 if _n==12
  replace min95= 0 if _n==12
    sort time
    gen y = 0
    gen x = 0
  save `unmatched', replace 

  use `matched', clear
  drop if _n>=12
  gen time = substr(parm,2,1)
  destring time, replace
  replace time = -time if substr(parm,1,1) =="b"

  expand 2 in 1
  replace time = -1 if _n==12
  replace est = 0 if _n==12
  replace max95 = 0 if _n==12
  replace min95= 0 if _n==12
  keep estimate time
  rename estimate estimate_matched

  merge 1:1 time using `unmatched'



  twoway (rarea min95 max95 time,  color(navy) fintensity(inten10) lcolor(navy) lpattern(dot)  ) ///
        (line estimate time, lcolor(navy) lpattern(dash) color(navy))  ///
        (line estimate_matched time, lcolor(navy) lpattern(solid) color(navy)) ///
        (function y=0, lcolor(black)  lpattern(solid) range(-5 6)) ///
        (function y = -0.5, lcolor(black) lpattern(solid)  range(-.05 .1) horizontal), ///
         xtitle("Years Relative to ZOC Expansion")  ///
         ytitle("College Enrollment Effect") ///
         xlabel( -5 -3 -1 1 3 5 , nogextend) ///
         ylabel(, nogextend) ///
         legend(off)

    
  graph export $figures/event_study_college_extended.pdf, replace
  graph export $figures/event_study_college_extended.tif, replace
  restore

end



************************************************************************************************
************************************************************************************************
* Figure 3d
*
************************************************************************************************
************************************************************************************************
capture program drop college_event_study_itt 
program define college_event_study_itt
syntax, [groupfe(string)]

  use $datadir/college_sample_itt.dta, clear 

  reghdfe four_year  b5_2-a6_2 hispanic black college poverty sped female migrant spanish_at_home  if ///
      endyear >=2008 & endyear<=2019, ///
      absorb(`groupfe' endyear  ) vce(cluster `groupfe' )  nocons level(95)

  preserve
    parmest, norestore

  drop if _n>=12
  gen time = substr(parm,2,1)
  destring time, replace
  replace time = -time if substr(parm,1,1) =="b"

  expand 2 in 1
  replace time = -1 if _n==12
  replace est = 0 if _n==12
  replace max95 = 0 if _n==12
  replace min95= 0 if _n==12
    sort time
    gen y = 0
    gen x = 0



  twoway (rarea min95 max95 time,  color(navy) fintensity(inten10) lcolor(navy) lpattern(dot)  ) ///
        (line estimate time, lcolor(navy) lpattern(solid) color(navy))  ///
        (function y=0, lcolor(black)  lpattern(dash_dot) range(-5 6)) ///
        (function y = -0.5, lcolor(black) lpattern(dash_dot)  range(-.05 .1) horizontal), ///
         xtitle("Years Relative to ZOC Expansion")  legend(off) ///
         ytitle("College Enrollment Effect") ///
         xlabel( -5 -3 -1 1 3 5)
  graph export $figures/event_study_college_zone_`groupfe'.pdf, replace
  restore

end 


************************************************************************************************
************************************************************************************************
* Figure 4
*
************************************************************************************************
************************************************************************************************
capture program drop vaQTE
program define vaQTE

  local script "$code/zoc_va_qte.R"
  * Run R
  shell /Library/Frameworks/R.framework/Resources/bin/R --vanilla <`script'

  * import data from R and make figure 
  import delimited $datadir/zoc_va_qte.csv, clear   
  twoway ///
        (rspike uci lci quantile, color(navy) fintensity(inten10) lcolor(navy) ) ///
        (line te quantile , color(navy) lpattern(solid)) ///
        (function y = 0, color(black)) , ///
        legend(off) ///
        ylabel(-0.4 -0.2 0 0.2 0.4) ///
        ytitle("School VA Effect ({&sigma})") ///
        xtitle("Quantile")

  graph export $figures/zoc_va_qte.pdf , replace 
end 


************************************************************************************************
************************************************************************************************
* Table 1
*
************************************************************************************************
************************************************************************************************
capture program drop descriptive_stats
program define descriptive_stats
syntax, [data(string) traditional(string) subjectMath(string)]

  * Make student-level balance table 
  use `data', clear 
  decode preferredlocationname, gen(preffname)
  gen missingAny = (missingLagE ==1) | (missingLagM ==1)
  drop white 
  gen white = ethnicity==10
  local chars "lagE lagM missingAny black hispanic white english_learner sped female migrant spanish_at_home poverty college"

  * restrict to event-study sample 
  drop if z_ela_all==0
  keep if endyear<=2019
  reghdfe z_ela_all post_zoc  if  maxYear==2019 & minYear<=2011  , ///
        absorb(preferredlocationcode endyear  ) vce(cluster preferredlocationcode  endyear )  nocons

  gen treat = zone!=""
  gen esSamp =  e(sample)==1



  texdoc init $tables/balance_final.tex, replace force
  texdoc write \begin{table}
  texdoc write \caption{Balance Table}
  texdoc write \begin{tabular}{lcccccc} \hline \hline
  texdoc write  & (1) & (2) & (3) & (4) & (5) & (6) \\
  texdoc write  & ZOC & Non-ZOC & Difference & Matched Non-ZOC & Difference  \\ \hline \\
  foreach char of local chars {
    reghdfe `char' treat if endyear>=2013 & esSamp==1 & maxYear==2019 & minYear<=2011 & endyear<=2019 , absorb(endyear) vce(cluster preferredlocationcode)
    local control = round(_b[_cons], .001)
    local treat = round(_b[_cons] + _b[treat], .001)

    local df = e(df_r)
    local diff = round(_b[treat], .001)
    local diff_se = round(_se[treat], .001)
    local diff_p = (2*ttail(`df', abs(_b[treat]/_se[treat]) ) )
    local diff_star ""
    if `diff_p ' <=.01 local diff_star "***"
    else if `diff_p '<=.05 & `diff_p ' >.01 local diff_star "**"
    else if `diff_p '<=.10 & `diff_p ' >.05 local diff_star "*"
    count if e(sample) ==1 & treat==0
    local N_c= r(N)
    count if e(sample) ==1 & treat==1
    local N_t= r(N)
    reghdfe `char' treat if endyear>=2013 & analysis_schools==1  & esSamp==1 & maxYear==2019 & minYear<=2011, absorb(endyear) vce(cluster preferredlocationcode)
    local df = e(df_r)
    local control_m = round(_b[_cons], .001)
    local diff_m = round(_b[treat], .001)
    local diff_m_se = round(_se[treat], .001)
    local diff_m_p = (2*ttail(`df', abs(_b[treat]/_se[treat]) ) )
    local diff_m_star ""
    if `diff_m_p ' <=.01 local diff_m_star "***"
    else if `diff_m_p '<=.05 & `diff_m_p ' >.01 local diff_m_star "**"
    else if `diff_m_p '<=.10 & `diff_m_p ' >.05 local diff_m_star "*"
    count if e(sample) ==1 & treat==0
    local N_c_m = r(N)




    if "`char'" =="lagE" local rowname "8th Grade ELA Scores"
    if "`char'" =="lagM" local rowname "8th Grade Math Scores"
    if "`char'" =="black" local rowname "Black Share"
    if "`char'" =="hispanic" local rowname "Hispanic"
    if "`char'" =="white" local rowname "White"
    if "`char'" =="english_learner" local rowname "English Learner"
    if "`char'" =="sped" local rowname "Special Education"
    if "`char'" =="female" local rowname "Female"
    if "`char'" =="migrant" local rowname "Migrant"
    if "`char'" =="spanish_at_home" local rowname "Spanish at home"
    if "`char'" =="poverty" local rowname "Poverty"
    if "`char'" =="college" local rowname "Parents College +"
    if "`char'" =="missingAny" local rowname "Missing Any Lagged Test Score"
    texdoc write `rowname' & `treat' & `control' & `diff'`diff_star' & `control_m' & `diff_m'`diff_m_star'  \\
    texdoc write & & & (`diff_se') & & (`diff_m_se')   \\

  }
  texdoc write Students & `N_t' & `N_c' & & `N_c_m' &  \\ \hline \hline
  texdoc write \end{tabular}
  texdoc write \end{table}
  texdoc close


  * make school-level descriptive table 
  keep if endyear ==2011
  collapse (mean) `chars' treat analysis_schools, by(preferredlocationcode )
  texdoc init $tables/school_balance_final.tex, replace force
  texdoc write \begin{table}
  texdoc write \caption{Balance Table}
  texdoc write \begin{tabular}{lccccc} \hline \hline
  texdoc write  & (1) & (2) & (3) & (4) & (5)  \\
  texdoc write  & ZOC & Non-ZOC & Difference & Matched Non-ZOC & Difference   \\ \hline \\
  foreach char of local chars {
    reghdfe `char' treat , noabsorb vce(robust)
    local control = round(_b[_cons], .001)
    local treat = round(_b[_cons] + _b[treat], .001)

    local df = e(df_r)
    local diff = round(_b[treat], .001)
    local diff_se = round(_se[treat], .001)
    local diff_p = (2*ttail(`df', abs(_b[treat]/_se[treat]) ) )
    local diff_star ""
    if `diff_p ' <=.01 local diff_star "***"
    else if `diff_p '<=.05 & `diff_p ' >.01 local diff_star "**"
    else if `diff_p '<=.10 & `diff_p ' >.05 local diff_star "*"
    count if e(sample) ==1 & treat==0
    local N_c= r(N)
    count if e(sample) ==1 & treat==1
    local N_t= r(N)
    reghdfe `char' treat if analysis_schools==1 , noabsorb vce(robust)
    local df = e(df_r)
    local control_m = round(_b[_cons], .001)
    local diff_m = round(_b[treat], .001)
    local diff_m_se = round(_se[treat], .001)
    local diff_m_p = (2*ttail(`df', abs(_b[treat]/_se[treat]) ) )
    local diff_m_star ""
    if `diff_m_p ' <=.01 local diff_m_star "***"
    else if `diff_m_p '<=.05 & `diff_m_p ' >.01 local diff_m_star "**"
    else if `diff_m_p '<=.10 & `diff_m_p ' >.05 local diff_m_star "*"
    count if e(sample) ==1 & treat==0
    local N_c_m = r(N)



    if "`char'" =="lagE" local rowname "8th Grade ELA Scores"
    if "`char'" =="lagM" local rowname "8th Grade Math Scores"
    if "`char'" =="black" local rowname "Black Share"
    if "`char'" =="hispanic" local rowname "Hispanic"
    if "`char'" =="white" local rowname "White"
    if "`char'" =="english_learner" local rowname "English Learner"
    if "`char'" =="sped" local rowname "Special Education"
    if "`char'" =="female" local rowname "Female"
    if "`char'" =="migrant" local rowname "Migrant"
    if "`char'" =="spanish_at_home" local rowname "Spanish at home"
    if "`char'" =="poverty" local rowname "Poverty"
    if "`char'" =="college" local rowname "Parents College +"
    texdoc write `rowname' & `treat' & `control' & `diff'`diff_star' & `control_m' & `diff_m'`diff_m_star'  \\
    texdoc write & & & (`diff_se') & & (`diff_m_se')   \\

  }
  texdoc write Students & `N_t' & `N_c' & & `N_c_m' &  \\ \hline \hline
  texdoc write \end{tabular}
  texdoc write \end{table}
  texdoc close 

end


************************************************************************************************
************************************************************************************************
* Table 2
*
************************************************************************************************
************************************************************************************************
capture program drop deltaRegressions
program define deltaRegressions
syntax, [robustness(string)]
  set seed 123456
  set sortseed 123456

  use $datadir/lausd2002_2021, clear
  keep if endyear >=2008
  * make sure to eventually assign to grade 11 obs 
  replace endyear = endyear + 3 
  gen score_low = z_ela_all <-.5 & !missing(z_ela_all)
  gen score_high = z_ela_all >.5 & !missing(z_ela_all)
  gen score_avg = score_low==0 & score_high==0
  gen hispanic = ethnicity==5
  gen black = ethnicity==3
  keep studentpseudoid endyear score_* gradecode 
  duplicates drop studentpseudoid endyear, force 
  tempfile scores
  save `scores'

  use $datadir/zoc_analysis_data_ela, clear
  duplicates drop studentpseudoid endyear , force 
  * merge baseline scores and groups
  merge 1:1 studentpseudoid endyear using `scores', gen(mergeScoreGroups) keep(3)
  replace score_avg = 1 if missing(score_avg)
  replace score_high = 0 if missing(score_high)
  replace score_low = 0 if missing(score_low)

  * make ability constant within school
  bys preferredlocationcode endyear: egen abilityschool = mean(ability)
  drop ability 
  rename abilityschool ability 
  * school by cell collapse 
  collapse (mean) ability ATE match (count) cellsize=studentpseudoid, by(preferredlocationcode score_low score_high score_avg endyear)
  drop if score_avg ==.
  gen schoolgroup = "avg" if score_avg==1
  replace schoolgroup = "low" if score_low==1
  replace schoolgroup = "high" if score_high==1

  * standardize variables 
  local vars "ability ATE match"
  foreach var of local vars{
    * Added by group here 
    if "`var'" =="match"{
    bys endyear  schoolgroup: egen mean`var' = mean(`var')
    bys endyear schoolgroup : egen sd`var' = sd(`var')
    gen z_`var' = (`var' - mean`var')/sd`var'
    }
    else{
        bys   endyear  : egen mean`var' = mean(`var')
        bys   endyear  : egen sd`var' = sd(`var')
        gen z_`var' = (`var' - mean`var')/sd`var'
    }
  }
  tempfile schoolchars
  save `schoolchars'

  * read in choice data and create school-level variables, namely school types 
  use $datadir/choiceData.dta, clear
  rename choice_plocn preferredlocationcode
  gen stem = regexm(schoolname, "STEM") | regexm(schoolname, "Science") | regexm(schoolname , "Math") | regexm(schoolname, "Technology") | regexm(schoolname, "Scientific") | regexm(schoolname, "Medical") | regexm(schoolname, "Medicine") | regexm(schoolname, "Biotech") | regexm(schoolname, "Health")
  gen sj = regexm(schoolname, "Social") | regexm(schoolname, "Justice") | regexm(schoolname, "Empowerment") | regexm(schoolname, "STEAM") | regexm(schoolname, "Humanitas") | regexm(schoolname, "Law")
  gen coll_academy = regexm(schoolname, "College") | regexm(schoolname, "University")
  gen business = regexm(schoolname, "Business")
  gen art = regexm(schoolname, "Art") | regexm(schoolname, "Photography") | regexm(schoolname, "Media")
  collapse (max) stem sj coll_academy business art, by(preferredlocationcode)
  tempfile stypes
  save `stypes'


  * read in estimated deltas 
  if "`robustness'"=="robust"{
    use $datadir/deltas2013_2019_update_dist_sq.dta, clear    
  }
  else{ 
    use $datadir/deltas2013_2019.dta, clear    
  }

  rename year endyear

  * ABC order: average, high, low
  gen score_avg= group=="avg"
  gen score_high= group=="high"
  gen score_low= group=="low"

  
  local vars "estimate"
  foreach var of local vars{
    * Normalizing within cell-year 
    bys group endyear  : egen mean`var' = mean(`var')
    bys group endyear : egen sd`var' = sd(`var')
    gen z_`var' = (`var' - mean`var')/sd`var'
  }
  gen year14 = endyear==2014
  * For school test scores and all, assign 2014 values to 2013 (due to missing test score year)
  replace endyear = 2013 if year14==1
  merge m:1 preferredlocationcode endyear score_avg score_high score_low  using `schoolchars', gen(mergeSchoolInfo) keep(1 3)
  replace endyear = 2014 if year14==1
  merge m:1 preferredlocationcode  using `stypes', gen(mergeTypes) keep(1 3)
  merge m:1 preferredlocationcode endyear using $datadir/school_attributes.dta, gen(mergeAttributes) keep(1 3)

  egen zone_year = group(zone endyear )

  * inverse of standard error
  gen inv = 1/stderr

  * deal with outside option -- assign high precision and normalize the covariates to zero 
  sum inv, detail 
  local max_inv = r(p99)
  replace inv = `max_inv' if inv==. | inv==0
  * normalize outside option attributes 
  replace ATE = 0 if estimate==0
  replace match=0 if estimate==0 
  replace ability = 0 if estimate==0

  replace zone = "Bell" if zone=="Bell/South Gate"
  replace zone = "BH" if zone =="Boyle Heights"
  egen cell_zone = group(group zone )

  * matrix to keep bootstrapped p-values 
  matrix define p = J(8,4,.)

  reghdfe z_estimate z_ATE [aweight=inv], absorb( cell_zone_year `fe'  ) vce(cluster cell_zone   )
  outreg2 * using $tables/delta_regressions_logit`robustness'.tex, replace  tex label
  boottest z_ATE , boottype(wild)
  matrix p[1,1] = 1
  matrix p[1,2] = r(p)
  reghdfe z_estimate z_ability [aweight=inv], absorb(cell_zone_year `fe'  ) vce(cluster cell_zone   )
  outreg2 * using $tables/delta_regressions_logit`robustness'.tex, append  tex label
  boottest z_ability , boottype(wild)
  matrix p[2,1] = 2
  matrix p[2,3] = r(p)
  reghdfe z_estimate z_match [aweight=inv], absorb(cell_zone_year `fe'  ) vce(cluster cell_zone   )
  outreg2 * using $tables/delta_regressions_logit`robustness'.tex, append  tex label
  boottest z_match , boottype(wild)
  matrix p[3,1] = 3
  matrix p[3,4] = r(p)
  reghdfe z_estimate z_ATE z_ability z_match [aweight=inv], absorb(zone_year `fe'  ) vce(cluster cell_zone   )
  outreg2 * using $tables/delta_regressions_logit`robustness'.tex, append  tex label
  boottest z_ATE , boottype(wild)
  matrix p[4,1] = 4
  matrix p[4,2] = r(p)
  boottest z_ability , boottype(wild)
  matrix p[4,3] = r(p)  
  boottest z_match , boottype(wild)
  matrix p[4,4] = r(p)  

  local controls "stem sj coll_academy art business teacherpsuedoid honors ap age cs science social mathematics  tchr_latino tchr_asian tchr_black"
  foreach ctrl of local controls{
    gen missing_`ctrl' = missing(`ctrl')
    replace `ctrl' = 0 if missing(`ctrl')
  }
  local controls "stem sj coll_academy art business teacherpsuedoid honors ap age cs science social mathematics  tchr_latino tchr_asian tchr_black"

  reghdfe z_estimate z_ATE `controls' missing_* [aweight=inv], absorb( cell_zone_year `fe'  ) vce(cluster cell_zone   )
  outreg2 * using $tables/delta_regressions_logit_controls`robustness'.tex, replace  tex label
  boottest z_ATE , boottype(wild)
  matrix p[5,1] = 5
  matrix p[5,2] = r(p)

  reghdfe z_estimate z_ability `controls' missing_* [aweight=inv], absorb(cell_zone_year `fe'  ) vce(cluster cell_zone   )
  outreg2 * using $tables/delta_regressions_logit_controls`robustness'.tex, append  tex label
  boottest z_ability , boottype(wild)
  matrix p[6,1] = 6
  matrix p[6,3] = r(p)

  reghdfe z_estimate z_match `controls' missing_* [aweight=inv], absorb(cell_zone_year `fe'  ) vce(cluster cell_zone   )
  outreg2 * using $tables/delta_regressions_logit_controls`robustness'.tex, append  tex label
  boottest z_match , boottype(wild)
  matrix p[7,1] = 7
  matrix p[7,4] = r(p)

  reghdfe z_estimate z_ATE z_ability z_match `controls' missing_* [aweight=inv], absorb(zone_year `fe'  ) vce(cluster cell_zone   )
  outreg2 * using $tables/delta_regressions_logit_controls`robustness'.tex, append  tex label
  boottest z_ATE , boottype(wild)
  matrix p[8,1] = 8
  matrix p[8,2] = r(p)
  boottest z_ability , boottype(wild)
  matrix p[8,3] = r(p)  
  boottest z_match , boottype(wild)
  matrix p[8,4] = r(p)  

  clear
  svmat p
  save $datadir/pvalues`robustness', replace 
end 



************************************************************************************************
************************************************************************************************
* Table 3
*
************************************************************************************************
************************************************************************************************
capture program drop ovgRobustness
program define ovgRobustness
syntax, [data(string)  subjectMath(string) postvar(string) group(string) outcome(string) yearfe(string)]

  import delimited "$rawdata/zoc_high_schools_list.csv", clear
  *rename ïpreferredlocationcode preferredlocationcode
  tempfile zoc_schools
  save `zoc_schools'

  use $datadir/ovg_micro_data_school, clear
  drop if missing(preferredlocationcode)
  merge 1:1 preferredlocationcode using `zoc_schools', gen(mergeZones) keep(1 3)
  sum lnovgschool, detail
  gen school_ovg_3_4 = lnovgschool >= r(p50) & !missing(school_ovg_estimate)
  tempfile school_ovg
  save `school_ovg'


  import delimited ///
    "$rawdata/la_06037_census_tract_income.csv", clear varnames(1) delimiters("|")
  format censustractid %20.0g
  tostring censustractid, replace
  replace censustractid = "0" + censustractid
  keep censustractid median_income
  duplicates drop censustractid, force
  tempfile tractovg
  save `tractovg'


  use `data', clear 
  gen lnovg= ln(ovg_estimate + 1)
  * Make census tract identifier from census block identifier
  decode censusblockid, gen(blockid_str)
  gen censustractid = substr(blockid_str, 1,11)
  drop blockid_str

  replace highOVG_1 = 0 if missing(highOVG_1)
  replace highOVG_2 = 0 if missing(highOVG_2)
  replace highOVG_3 = 0 if missing(highOVG_3)
  replace highOVG_4 = 0 if missing(highOVG_4)
  gen post_zoc_highOVG_3 = post_zoc * highOVG_3
  gen post_zoc_highOVG_4 = post_zoc * highOVG_4
  gen post_zoc_highOVG_3_4 = (post_zoc_highOVG_3==1) | (post_zoc_highOVG_4 ==1)
  replace ovg_estimate = 0 if missing(ovg_estimate)

  * need to back out high ovg students before the policy intervention. variation is at block level so this suffices
  gen highOVG_2_4 = (highOVG_2==1) | (highOVG_3==1) | (highOVG_4==1)
  bys censusblockid: egen highOVG_block = max(highOVG_2_4)
  replace highOVG_2_4=1 if highOVG_2_4==0 & highOVG_block==1
  gen highOVG_3_4 = (highOVG_3==1) | (highOVG_4==1)
  bys censusblockid: egen highOVG_block34 = max(highOVG_3_4)
  replace highOVG_3_4=1 if highOVG_3_4==0 & highOVG_block34==1

  merge m:1 preferredlocationcode using `school_ovg', gen(schoolOVG) keep(1 3)
  replace school_ovg_3_4= 0 if missing(school_ovg_3_4)
  gen post_zoc_high_school_ovg_3_4 = post_zoc *(school_ovg_3_4==1)

  * Merge censustract income
  merge m:1 censustractid using `tractovg', gen(mergeTractIncome) keep(1 3)
  gen logtractincome = log(median_income )
  replace logtractincome = 0 if missing(logtractincome)
  gen missingincome = logtractincome==0

  label var z_ela_all "Reading"
  label var z_math_all "Math"
  label var post_zoc "PostZOC"
  label var post_zoc_highOVG_3_4 " PostZOC $\times OVI_{3,4}$"
  label var post_zoc_high_school_ovg_3_4 " PostZOC $\times School $OVG_{3,4}$"
 
  * fix zone labeling for zone-year clustering 
  replace zone = "LAUSD" if missing(zone)
  replace zone = "Bell" if zone=="Bell/South Gate"
  egen zone_year = group(zone endyear )

  * school-level ovg heterogeneity variables 
  gen ovgestimate = school_ovg_estimate 
  replace ovgestimate = 0 if missing(lnovgschool )
  gen post_schoolovgestimate = (endyear>=2013)*ovgestimate

  * individiaul-level heterogeneity variables 
  gen postindividualovg = (endyear>=2013)*ovg_estimate 
  gen postlnovg = (endyear>=2013)*lnovg

  * same restriction as other regressions
  drop if `outcome'==0

  reghdfe `outcome'   post_zoc  `postvar'   ///
          migrant hispanic black college poverty sped female spanish_at_home  ///
          logtractincome missingincome   lagE lagM `group' ///
          if  maxYear==2019 & minYear<=2011 & analysis_schools==1 & endyear>=2008 , ///
          absorb(preferredlocationcode `yearfe' ) vce(cluster preferredlocationcode    )
  outreg2 * using $tables/ovg_school_robustness_`postvar'_`outcome'.tex, replace  tex label keep(post_zoc  `postvar' ) dec(3)

  * start adding in additional post interactions
  local vars "female black hispanic poverty college migrant spanish_at_home sped lagE "
  foreach var of local vars{
      gen inter_`var' = post_zoc*`var'
      label var inter_`var' "\ \ \ \ \ \ `var'"
  }

  * Add sex
  reghdfe `outcome'  post_zoc  `postvar'  inter_female ///
              migrant hispanic black  college poverty sped female spanish_at_home ///
              logtractincome missingincome  lagE lagM `group' ///
            if  maxYear==2019 & minYear<=2011 & analysis_schools==1 & endyear>=2008 , ///
            absorb(preferredlocationcode `yearfe'  ) vce(cluster preferredlocationcode   )


  outreg2 * using $tables/ovg_school_robustness_`postvar'_`outcome'.tex, append  tex label keep(post_zoc `postvar'  inter_female) dec(3)

  * Add race
  reghdfe `outcome' `prevar'  `postvar'  ///
              inter_black inter_hispanic ///
              migrant hispanic black  college poverty sped female spanish_at_home ///
              logtractincome missingincome   lagE lagM `group' ///
            if  maxYear==2019 & minYear<=2011 & analysis_schools==1 & endyear>=2008 , ///
            absorb(preferredlocationcode `yearfe'  ) vce(cluster preferredlocationcode   )
  outreg2 * using $tables/ovg_school_robustness_`postvar'_`outcome'.tex, append  tex label keep(`prevar'  `postvar' inter_* ) dec(3)

  * Add SES
  reghdfe `outcome' post_zoc  `postvar'  ///
              inter_poverty inter_college inter_migrant inter_spanish_at_home inter_sped  ///
              migrant hispanic black  college poverty sped female spanish_at_home ///
              logtractincome missingincome   lagE lagM  `group' ///
            if  maxYear==2019 & minYear<=2011 & analysis_schools==1 & endyear>=2008 , ///
            absorb(preferredlocationcode `yearfe'  ) vce(cluster preferredlocationcode   )
  outreg2 * using $tables/ovg_school_robustness_`postvar'_`outcome'.tex, append  tex label keep(post_zoc  `postvar'  inter_* ) dec(3)


  * Add lagged scores
  reghdfe `outcome' post_zoc  `postvar'   ///
              inter_lagE ///
              migrant hispanic black  college poverty sped female spanish_at_home ///
              logtractincome missingincome   lagE lagM `group' ///
            if  maxYear==2019 & minYear<=2011 & endyear>=2008 & analysis_schools==1, ///
            absorb(`yearfe' preferredlocationcode ) vce(cluster preferredlocationcode   )
  outreg2 * using $tables/ovg_school_robustness_`postvar'_`outcome'.tex, append  tex label keep(post_zoc  `postvar' inter_* ) dec(3)

  * Add all post interactions
  reghdfe `outcome' post_zoc  `postvar'  inter_* ///
              migrant hispanic black  college poverty sped female spanish_at_home ///
              logtractincome missingincome   lagE lagM  `group' ///
            if  maxYear==2019 & minYear<=2011 & analysis_schools==1 & endyear>=2008 , ///
            absorb(preferredlocationcode `yearfe'  ) vce(cluster preferredlocationcode   )
  outreg2 * using $tables/ovg_school_robustness_`postvar'_`outcome'.tex, append  tex label keep(post_zoc  `postvar'   inter_* ) dec(3)

  reghdfe `outcome' post_zoc  `postvar'  inter_* ///
              migrant hispanic black  college poverty sped female spanish_at_home ///
              logtractincome missingincome   lagE lagM  `group' ///
            if  maxYear==2019 & minYear<=2011 & analysis_schools==1 & endyear>=2008 , ///
            absorb(preferredlocationcode zone_year ) vce(cluster preferredlocationcode   )
  outreg2 * using $tables/ovg_school_robustness_`postvar'_`outcome'.tex, append  tex label keep(post_zoc  `postvar'  ) dec(3)



 
end



************************************************************************************************
************************************************************************************************
* Table 4
*
************************************************************************************************
************************************************************************************************
capture program drop intermediateOutcomes
program define intermediateOutcomes
syntax, [data(string) ]


  use `data', clear 
  * use same sample as the rest of the paper 
  keep if endyear<=2019 

  * merge in end-of-HS behavior and other outcome aggregates 
  merge m:1 studentpseudoid using ${datadir}/student_behavior_data.dta, keep(1 3) gen(mergeBehavior)
  replace tookSAT = 0 if missing(tookSAT)
  replace college_complete_status = 0 if missing(college_complete_status)
  replace total_suspension_days = 0 if missing(total_suspension_days)
  replace total_ap = 0 if missing(total_ap)
  replace total_honors = 0 if missing(total_honors)
  replace total_incidents = 0 if missing(total_incidents)

  gen tookSAT_morethanonce = numSAT_took >1 & !missing(numSAT_took)
  egen total_sat = rowtotal(max_math_sat max_verbal_sat max_write_sate)
  replace total_sat = . if total_sat==0 

  gen pre_zoc = (endyear>=2008 & endyear<=2011)*(zoc_hs==1)



  texdoc init $tables/mechanisms_table.tex, replace force
  texdoc write \begin{table}
  texdoc write \caption{Potential Mechanisms}
  texdoc write \begin{tabular}{lcccc} \hline \hline
  texdoc write  & (1) & (2) & (3) & (4)  \\
  texdoc write  & N & $\bar{Y}$ & Pre $\times$ ZOC & Post $\times$ ZOC  \\ \hline \\
  texdoc write  & & & & \\
  texdoc write  & & \multicolumn{2}{c}{Panel A: Behavior} \\
  texdoc write  & & & & \\
  local behavior "total_incidents total_suspension_days total_days_absent"
  foreach b of local behavior{
    if "`b'"=="total_incidents" local rowname "Suspension Incidents"
    if "`b'"=="total_suspension_days" local rowname "Suspension Days"
    if "`b'"=="total_days_absent" local rowname "Total Absent Days"
    reghdfe `b'   pre_zoc post_zoc  if   maxYear==2019 & minYear<=2011  & endyear >=2008 & analysis_schools==1, ///
      absorb(preferredlocationcode endyear  ) vce(cluster preferredlocationcode  )  nocons
    local post : di %9.3f round(_b[post_zoc], .001)
    local post_se: di %9.3f round(_se[post_zoc], .001)
    local pre : di %9.3f round(_b[pre_zoc], .001)
    local pre_se : di %9.3f round(_se[pre_zoc], .001)
    local N = e(N)
    sum `b' if endyear==2012
    local ybar : di %9.3f round(r(mean), .001) 

    texdoc write `rowname' &  `N' & `ybar' & `pre' & `post' \\
    texdoc write & & & (`pre_se') & (`post_se') \\
  } 
  texdoc write  & & & & \\
  texdoc write  & & \multicolumn{2}{c}{Panel B: College Preparation} \\
  texdoc write  & & & & \\
  local college "college_met_status tookSAT total_sat max_math_sat max_verbal_sat max_write_sate "
  foreach b of local college{
    if "`b'"=="college_met_status" local rowname "Met UC-CSU Requirements"
    if "`b'"=="tookSAT" local rowname "Took SAT"
    if "`b'"=="total_sat" local rowname "SAT Score"
    if "`b'"=="max_write_sate" local rowname "Writing SAT Score"
    if "`b'"=="max_verbal_sat" local rowname "Verbal SAT Score"
    if "`b'"=="max_math_sat" local rowname "Math SAT Score"
    reghdfe `b'   pre_zoc post_zoc  if   maxYear==2019 & minYear<=2011  & endyear >=2008 & analysis_schools==1, ///
      absorb(preferredlocationcode endyear  ) vce(cluster preferredlocationcode  )  nocons
    local post : di %9.3f round(_b[post_zoc], .001)
    local post_se: di %9.3f round(_se[post_zoc], .001)
    local pre : di %9.3f round(_b[pre_zoc], .001)
    local pre_se : di %9.3f round(_se[pre_zoc], .001)
    local N = e(N)
    sum `b' if endyear==2012
    local ybar : di %9.3f round(r(mean), .001) 

    texdoc write `rowname' & `N' & `ybar'  & `pre' & `post' \\
    texdoc write & & & (`pre_se') & (`post_se') \\
  } 
  texdoc write & & & & \\ \hline \hline 
  texdoc write \end{tabular}
  texdoc write \end{table}
  texdoc close

end 

main 

timer off 1 
timer list 1 



